const currency = function (num, plusMinus, zero, places, sym, thousand, decimal) {
  if (num === null || num === undefined || num === 0){
    if (zero) return '0.00'
    else return ''
  };
  places = !isNaN(places = Math.abs(places)) ? places : 2;
  sym = sym !== undefined ? sym : "";
  thousand = thousand || ",";
  decimal = decimal || ".";
  var negative = num < 0 ? "-" : "",
    i = parseInt(num = Math.abs(+num || 0).toFixed(places), 10) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
  if (plusMinus) negative = "";
  return sym + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(num - Number(i)).toFixed(places).slice(2) : "");
}


const handleNavigationBar = title => {
  wx.setNavigationBarTitle({
    title: title
  })
}

// 点击下载
const download = function(url){
  wx.downloadFile({
    url: url,
    header: {},
    success: function (res) {
      wx.saveFile({
        tempFilePath: res.tempFilePath,
        success: function (res) {
          console.log('保存文档成功')
          var savedFilePath = res.savedFilePath
          console.log(savedFilePath)
          wx.showModal({
            title: '提示',
            content: `下载成功,是否打开文件？`,
            success: function (res) {
              if (res.confirm) {
                wx.openDocument({
                  filePath: savedFilePath,
                  fileType: 'pdf',
                  success: function (res) {
                    console.log('打开文档成功')
                  },
                  fail: function () {
                    console.log('打开文档失败')
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        },
        fail: function () {
          console.log('保存文档失败')
        }
      })
    },
    fail: function (res) {
      console.log('下载失败')
    },
  })
}

module.exports = {
  currency: currency,
  handleNavigationBar: handleNavigationBar,
  download: download
}
